/**
 * Sider Menu Module.
 */
define(['jquery','jquery.cookie'], function($, cookie) {
	var SiderMenu;
	SiderMenu = (function() {
		var calculateFixedSidebarViewportHeight;
		SiderMenu = function() {
			this.init();
		}
		calculateFixedSidebarViewportHeight = function () {
      var sidebarHeight = VAM.getViewport().height;
      // console.log(sidebarHeight);
      return sidebarHeight;
    };
		SiderMenu.prototype.setActiveNav = function() {
			var activeNav = $('#curr-menu').val(),
				$activeNav = $('#'+activeNav);
			$activeNav.siblings('li').removeClass('active');
			$activeNav.addClass('active');
			$activeNav.find('span > .fa').addClass('fa-chevron-down');
			$activeNav.find('.sub-nav').show();
		};
		SiderMenu.prototype.setCurrentNav = function() {
			var currNav = $('#curr-nav').val(),
				$currNav = $('#'+currNav);
			$('.accordion ul > li').removeClass('current');
			$currNav.addClass('current');
		};
		SiderMenu.prototype.toggleNav = function() {
			var $topMenuItem = $('.accordion ul > li a.has-sub'),
				$siderbar = $('#global-sider-menu'),
				$mainContainer = $('.main-container'),
				minHeight = 600,
			viewport = VAM.getViewport();
			$topMenuItem.on('click', function(evt) {
				var $me = $(this),
					$parent = $me.parent(),
					$siblings = $parent.siblings('li'),
					$icon = $me.find('span i'),
					$subNav = $me.siblings('.sub-nav'),
					$siblingsIcon = $siblings.find('span i'),
					$siblingsSubNav = $siblings.find('.sub-nav'),
					menuTop, top;
				evt.preventDefault();
				$siblings.removeClass('open');
				$parent.addClass('open');
				$icon.toggleClass('fa-chevron-down');
				$siblingsIcon.removeClass('fa-chevron-down');
				$siblingsSubNav.slideUp('fast');
				$subNav.slideToggle('fast', function() {
					if (viewport.width >= 992) {
						if ($siderbar.height() > 1000) {
							minHeight = $siderbar.height();
							$mainContainer.css('min-height', minHeight);
						}
						// if ($('body').hasClass('page-sider-closed') === false) {
						// 	menuTop = $me.position().top;
				  //     top = document.documentElement.scrollTop || document.body.scrollTop;
				  //     $('html, body').stop().animate({
						// 		scrollTop: (menuTop - top) / 2
						// 	}, 'slow');
						// }
		      }
				});
			});
		};
		SiderMenu.prototype.toggleSiderbar = function() {
			var $sidebarToggle = $('.sider-menu-toggle'),
				$body = $('body');
			if ($.cookie('vam_dsp_page_sider_closed') === undefined) {
				$.cookie('vam_dsp_page_sider_closed', 'false', {path: '/' });
			};
			// console.log($.cookie('vam_dsp_page_sider_closed'));
			$sidebarToggle.on('click', function(evt) {
				var vamPageClosedFlag = $.cookie('vam_dsp_page_sider_closed');
				evt.preventDefault();
				// console.log(vamPageClosedFlag);
				$body.toggleClass('page-sider-closed');
				if (!$body.hasClass('page-sider-closed')) {
					$('#global-sider-menu').css('width', '');
				}
				// $.removeCookie('name');
				if (vamPageClosedFlag === 'false') {
					$.cookie('vam_dsp_page_sider_closed', 'true', {path: '/' });
				} else {
					$.cookie('vam_dsp_page_sider_closed', 'false', {path: '/' });
				}
				// Redraw dashboard data
				try {
					$('#traffic-income-chart').highcharts().reflow();
					$('#dsp-graph-container').highcharts().reflow();
					$('#media-graph-container').highcharts().reflow();
				} catch (e) {
					// console.log(e.message);
				}
				// console.log($.cookie('vam_dsp_page_sider_closed'));
			});
		};
		SiderMenu.prototype.pushSiderMenu = function() {
			var $mainContainer = $('.main-container'),
				$siderNavBar = $('.sidebar-nav'),
				$slideMenuLeft = $('.slide-menu-left'),
			sideMenuOpen,sideMenuClose;
			// Slide menu out of view
		  function sideMenuClose() {
		    $mainContainer.stop()
		    .animate({
		        'left': '0'
		    }, 250, 'swing');
		    $siderNavBar.stop()
		    .animate({
		        'left': '-250px'
		    }, 250, 'swing').removeClass('push-left');
		    $mainContainer.off('click');
		    if(typeof handler != 'undefined'){
		      $(window).unbind('resize', handler);
		    }
		  }
		  // Slide Menu in
		  function sideMenuOpen() {
		    $mainContainer.stop()
		    .animate({
		        'left': '250px'
		    }, 250, 'swing');
		    $siderNavBar.stop()
		    .animate({
		        'left': '0'
		    }, 250, 'swing').addClass('push-left');
		  }
		  // Slide Operate
		  $slideMenuLeft.on('click', function() {
		    if($siderNavBar.hasClass('push-left')){
		      sideMenuClose();
		    }else{
		      sideMenuOpen();
		      $mainContainer.on('click', function() {
		        sideMenuClose();
		      });
		      var handler = function() {
		  			sideMenuOpen();
		  		};
			    $(window).on('resize', handler);
		    }
		  });
		};
		SiderMenu.prototype.hoverSiderMenu = function () {
			var $siderBar = $('#global-sider-menu'),
				$body = $('body'),
				viewportWidth = VAM.getViewport().width;

			$siderBar.off("mouseenter").on("mouseenter", function() {
				if ($body.hasClass("page-sider-closed") && (viewportWidth >= 959)) {
					$siderBar.stop()
					.animate({
		        'width': '200px'
			    }, 200, function() {
			    	$siderBar.addClass('on');
			    });
				}
			});

			$siderBar.off("mouseleave").on("mouseleave", function() {
				if ($body.hasClass("page-sider-closed") && (viewportWidth >= 959)) {
					$siderBar.removeClass('on');
					$siderBar.find('.sub-nav').hide();
					$siderBar.stop()
					.animate({
		        'width': '50px'
			    }, 200, function() {

			    });
				}
			});
		}
		SiderMenu.prototype.init = function() {
			// this.setActiveNav();
			// this.setCurrentNav();
			this.toggleNav();
			// this.pushSiderMenu();
			this.toggleSiderbar();
			this.hoverSiderMenu();
		};

		return SiderMenu;
	})();
	
	return new SiderMenu();
});